package com.wr.online.test.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.wr.online.test.mapper.ResumeMapper;
import com.wr.online.test.model.entity.Resume;
import com.wr.online.test.model.vo.RespBean;
import com.wr.online.test.model.vo.ResumeVO;
import com.wr.online.test.util.MailUtil;
import com.wr.online.test.util.RestTemplateUtils;
import com.wr.online.test.validator.ValidationGroups;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import java.security.GeneralSecurityException;

/**
 * @author ：GZHU_WANG
 * @date ：Created in 2021/10/28 23:08
 * @description：
 */
@RestController
@RequestMapping("/resume")
public class ResumeController {

    @Autowired
    private ResumeMapper resumeMapper;
    @Autowired
    private RestTemplateUtils restTemplateUtils;

    @GetMapping("/{page}/{size}")
    public RespBean getPage(@PathVariable Integer page, @PathVariable Integer size){

        IPage<Resume> resumeIPage = resumeMapper.selectJoinPage(new Page<>(page, size), Resume.class, new MPJLambdaWrapper<Resume>()
                .selectAll(Resume.class)
                .orderByAsc(Resume::getLaunchTime, Resume::getLaunchTime)
                .orderByDesc(Resume::getState, Resume::getState)
        );
        return RespBean.success(resumeIPage);
    }


    @GetMapping("/{id}")
    public RespBean getResume(@PathVariable Integer id){
        Resume resume = resumeMapper.selectById(id);
        return RespBean.success(resume);
    }

    @PostMapping
    public RespBean postResume(@Validated({ValidationGroups.Insert.class}) @RequestBody ResumeVO resumeVO){
        Resume resume = new Resume();
        BeanUtils.copyProperties(resumeVO,resume);
        resumeMapper.insert(resume);
        return RespBean.success();
    }


    @PostMapping("/{id}")
    public RespBean postResume(@PathVariable Integer id) throws GeneralSecurityException, MessagingException {
        Resume resume = resumeMapper.selectById(id);

        String email = resume.getEmail();
        System.out.println(resume);

        Integer paperId = restTemplateUtils.httpGetPathVariable("http://192.168.3.131:8001/test/getPaper/10/5/3/"+id, Integer.class,"");
        MailUtil.sendMsg(email,paperId);
        return RespBean.success();
    }




}
